package com.tuanzi.loan.business.repository.application.impl;

import com.tuanzi.loan.business.repository.application.custom.LoanApplicationExportRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.application.LoanApplicationExportRequest;
import com.tuanzi.loan.vo.response.application.LoanApplicationExportResponse;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static com.tuanzi.loan.generated.jooq.loan.Tables.*;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanApplicationExport.LOAN_APPLICATION_EXPORT;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanApplicationExportPlatform.LOAN_APPLICATION_EXPORT_PLATFORM;

/**
 * @Auther: hequnayu
 * @Date: 2019/7/31 14:40
 * @Description:
 */
public class LoanApplicationExportRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanApplicationExportRepositoryCustom {

    @Override
    public Page<LoanApplicationExportResponse> list(LoanApplicationExportRequest searchRequest, Pageable pageable) {
        fields(LOAN_APPLICATION_EXPORT.ID,LOAN_APPLICATION_EXPORT.TITLE,LOAN_APPLICATION_EXPORT.NAME.as("exportName"),LOAN_APPLICATION_EXPORT.POSITION ,LOAN_APPLICATION_EXPORT.TYPE, LOAN_APPLICATION_EXPORT.IMGURL, LOAN_APPLICATION_EXPORT.AF_CLICK_IMGURL, LOAN_APPLICATION_EXPORT.STATUS,
                LOAN_APPLICATION_EXPORT_GROUP_PAGE.NAME,DSL.groupConcatDistinct(LOAN_APPLICATION_EXPORT_GROUP.NAME).as("groupName"),DSL.groupConcatDistinct(LOAN_APPLICATION_EXPORT_GROUP.ID).as("groupId"),
                DSL.groupConcatDistinct(COMM_PRODUCT_PRDID.NAME).separator("  ||  ").as("products"),
                DSL.groupConcatDistinct(LOAN_APPLICATION_EXPORT_PLATFORM.PLATFORM).separator("<br/>").as("platforms"));
        leftJoin(LOAN_APPLICATION_EXPORT_PRODUCT, LOAN_APPLICATION_EXPORT.ID.eq(LOAN_APPLICATION_EXPORT_PRODUCT.EXPORT_ID));
        leftJoin(COMM_PRODUCT_PRDID, COMM_PRODUCT_PRDID.PRDID.eq(LOAN_APPLICATION_EXPORT_PRODUCT.PRODUCT_ID));
        leftJoin(LOAN_APPLICATION_EXPORT_EXCLUDE_CHANNEL, LOAN_APPLICATION_EXPORT.ID.eq(LOAN_APPLICATION_EXPORT_EXCLUDE_CHANNEL.EXPORT_ID));
        leftJoin(LOAN_APPLICATION_EXPORT_PLATFORM, LOAN_APPLICATION_EXPORT.ID.eq(LOAN_APPLICATION_EXPORT_PLATFORM.EXPORT_ID));
        leftJoin(LOAN_APPLICATION_EXPORT_GROUP,LOAN_APPLICATION_EXPORT.EXPORT_GROUP_ID.eq(LOAN_APPLICATION_EXPORT_GROUP.GROUP_ID).and(LOAN_APPLICATION_EXPORT.TYPE.eq(LOAN_APPLICATION_EXPORT_GROUP.TYPE)));
        leftJoin(LOAN_APPLICATION_EXPORT_GROUP_PAGE,LOAN_APPLICATION_EXPORT_GROUP.PAGE_ID.eq(LOAN_APPLICATION_EXPORT_GROUP_PAGE.ID));

        in(LOAN_APPLICATION_EXPORT_PRODUCT.PRODUCT_ID, searchRequest.getProducts());
        in(LOAN_APPLICATION_EXPORT_PLATFORM.PLATFORM, searchRequest.getPlatforms());
        eq(LOAN_APPLICATION_EXPORT.TYPE, searchRequest.getType());
        eq(LOAN_APPLICATION_EXPORT_GROUP.ID, searchRequest.getExportGroupId());
        eq(LOAN_APPLICATION_EXPORT.STATUS, searchRequest.getStatus());
        ge(LOAN_APPLICATION_EXPORT.START_TIME, searchRequest.getStartTime());
        le(LOAN_APPLICATION_EXPORT.END_TIME, searchRequest.getEndTime());
        ge(LOAN_APPLICATION_EXPORT.MIN_VERSION_CODE, searchRequest.getMinVersion());
        le(LOAN_APPLICATION_EXPORT.MAX_VERSION_CODE, searchRequest.getMaxVersion());
        in(LOAN_APPLICATION_EXPORT_EXCLUDE_CHANNEL.CHANNEL_ID, searchRequest.getChannels());
//        contains(LOAN_APPLICATION_EXPORT.TITLE, searchRequest.getTitle());
        contains(LOAN_APPLICATION_EXPORT.NAME, searchRequest.getName());
        groupBy(LOAN_APPLICATION_EXPORT.ID);
//        orderBy(LOAN_APPLICATION_EXPORT.ORDER_NUM, Sort.Direction.ASC);
        return search(pageable, new ApplicationExportSearchResponseRecordMapper());
    }

    private final class ApplicationExportSearchResponseRecordMapper implements RecordMapper<Record, LoanApplicationExportResponse> {

        @Override
        public LoanApplicationExportResponse map(Record record) {
            LoanApplicationExportResponse response = new LoanApplicationExportResponse();
            response.setProducts(record.get("products") == null ? null : record.get("products").toString());
            response.setPlatforms(record.get("platforms") == null ? null : record.get("platforms").toString());
            response.setType(record.get(LOAN_APPLICATION_EXPORT.TYPE));
            response.setPageName(record.get(LOAN_APPLICATION_EXPORT_GROUP_PAGE.NAME));
            response.setGroupName(record.get("groupName").toString());
            response.setImgBefore(record.get(LOAN_APPLICATION_EXPORT.IMGURL));
            response.setImgAfter(record.get(LOAN_APPLICATION_EXPORT.AF_CLICK_IMGURL));
            response.setStatus(record.get(LOAN_APPLICATION_EXPORT.STATUS));
//            response.setOrderNum(record.get(LOAN_APPLICATION_EXPORT.ORDER_NUM));
//            response.setTitle(record.get(LOAN_APPLICATION_EXPORT.TITLE));
            response.setName(record.get("exportName").toString() == null ? "":record.get("exportName").toString());
            response.setPosition(record.get(LOAN_APPLICATION_EXPORT.POSITION));
            response.setId(record.get(LOAN_APPLICATION_EXPORT.ID));
            return response;
        }
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_APPLICATION_EXPORT;
    }
}
